#include<stdio.h>
#include <stdlib.h>
#include<unistd.h>
#include<sys/wait.h>


int main(int argc, char *argv[])

{
	pid_t pid, wpid;
	pid = fork();
	int status;

	if (pid == 0){
		printf("I am child, my id = %d, going to sleep 10s \n", getpid());
		sleep(10);
		printf("--------------child gong to die----------------\n");
		return 73;

	}else if (pid > 0) {
		// wpid = wait(NULL);  // 不关心子进程结束原因。
		wpid = wait(&status);  // 如果子进程未终止，父进程阻塞。
		if (wpid == -1){
			perror("wait error");
			exit(1);
		}
		if (WIFEXITED(status)){ // 为真，说明子进程正常终止。
			printf("child exit with: %d\n", WEXITSTATUS(status));
		}
		if (WIFSIGNALED(status)) {  // 为真，说明子进程是被信号终止。
			printf("child kill with signal %d\n", WTERMSIG(status));
		}
		printf("------parent wait finish, child pid: %d-------\n", wpid);
		printf("======status: %d======\n",status);
	}else {
		perror("fork error");
		return 1;
	}
	return 0;
}
